package cn.com.dhc.danlu.widget.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.CommonConst;
import cn.com.dhc.danlu.util.StringUtil;
import cn.com.dhc.danlu.widget.DLToolbar;
import cn.com.hd.mall.web.webservices.entity.response.shoppingcart.MerchWraper;
import cn.com.hd.mall.web.webservices.entity.response.shoppingcart.WrapPromotionBean;

/**
 * author: 李一方(<a href="mailto:liyifang@danlu.com">liyifang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-03-14 17:01
 *
 * <p>
 * 促销信息展示区
 * </p>
 */
// TODO: 2017/3/17 使用 popupwindow 重写 @lyf
public class PromotionDetailDialog extends Dialog implements ViewTreeObserver.OnGlobalLayoutListener {
    private View rootView;
    private TextView tvGift;
    private TextView tvPrice;
    private DLToolbar dlToolbar;
    private LinearLayout llGift;
    private LinearLayout llPrice;
    private LinearLayout llGiftArea;
    private LinearLayout llPriceArea;

    public PromotionDetailDialog(@NonNull Context context) {
        super(context, R.style.dialog_style);

        ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
        rootView = LayoutInflater.from(context).inflate(R.layout.view_shopcar_promotion_detail, decorView, false);
        setContentView(rootView);
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(this);

        initViews();
    }

    private void initViews() {
        dlToolbar = (DLToolbar) findViewById(R.id.dl_toolbar);
        tvGift = (TextView) rootView.findViewById(R.id.tv_gift);
        tvPrice = (TextView) rootView.findViewById(R.id.tv_price);
        llGift = (LinearLayout) rootView.findViewById(R.id.ll_gift);
        llPrice = (LinearLayout) rootView.findViewById(R.id.ll_price);
        llGiftArea = (LinearLayout) rootView.findViewById(R.id.ll_gift_area);
        llPriceArea = (LinearLayout) rootView.findViewById(R.id.ll_price_area);

        dlToolbar.setRightClickListenter(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    /**
     * 刷新数据 & 视图
     *
     * @param promotionBean
     *         促销数据
     */
    @SuppressLint("SetTextI18n")
    public void refreshContent(@NonNull WrapPromotionBean promotionBean) {
        if (TextUtils.isEmpty(promotionBean.getMsgReduce())) {
            llPrice.setVisibility(View.GONE);
            llPriceArea.setVisibility(View.GONE);
        } else {
            llPrice.setVisibility(View.VISIBLE);
            llPriceArea.setVisibility(View.VISIBLE);
            tvPrice.setText("促销优惠: 减 ￥" + promotionBean.getMsgReduce() + "元");
        }

        if (TextUtils.isEmpty(promotionBean.getMsgGift())) {
            llGift.setVisibility(View.GONE);
            llGiftArea.setVisibility(View.GONE);
        } else {
            llGift.setVisibility(View.VISIBLE);
            llGiftArea.setVisibility(View.VISIBLE);
            tvGift.setText("促销赠送: " + promotionBean.getMsgGift());
        }

        for (MerchWraper merchWraper : promotionBean.getMerchWraperList()) {
            if (CommonConst.PromotionConst.PROMOTION_CHANGE_TYPE_0.equals(merchWraper.getPromotionType())) {
                // 满赠
                if (merchWraper.getGiftList() != null) {
                    llGiftArea.addView(getItemView(merchWraper));
                }
            } else {
                // 满减
                llPriceArea.addView(getItemView(merchWraper));
            }
        }
    }

    private LinearLayout getItemView(MerchWraper merch) {
        LinearLayout llItem = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.item_shopcar_promotion_detail, null, false);
        TextView tvType = (TextView) llItem.findViewById(R.id.tv_type);
        TextView tvDetail = (TextView) llItem.findViewById(R.id.tv_detail);
        switch (merch.getPromotionType()) {
            case CommonConst.PromotionConst.PROMOTION_CHANGE_TYPE_0:
                // 满赠
                tvType.setText("【满赠】");
                break;

            case CommonConst.PromotionConst.PROMOTION_CHANGE_TYPE_1:
                // 满减
                tvType.setText("【满减】");
                break;

            case CommonConst.PromotionConst.PROMOTION_CHANGE_TYPE_4:
                // 限购
                tvType.setText("【限促】");
                break;

            default:
                break;
        }
        tvDetail.setText(StringUtil.hightLight(merch.getMerchName() + "  (" + merch.getPromotionInfo() + ")", ContextCompat.getColor(getContext(), R.color.color_b12525), (int) tvDetail.getTextSize()));
        return llItem;
    }

    @Override
    public void onGlobalLayout() {
        try {
            int measuredHeight = rootView.getMeasuredHeight();
            WindowManager m = getWindow().getWindowManager();
            Display d = m.getDefaultDisplay();
            if (measuredHeight > d.getHeight() / 2) {
                // 对话框最大高度不能超过屏幕一半
                ViewGroup.LayoutParams params = rootView.getLayoutParams();
                params.height = d.getHeight() / 2;
                rootView.setLayoutParams(params);
                rootView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        } catch (Exception ignored) {
            // no op..
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window dialogWindow = getWindow();
        if (dialogWindow != null) {
            dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
            WindowManager.LayoutParams p = dialogWindow.getAttributes();
            WindowManager m = getWindow().getWindowManager();
            Display d = m.getDefaultDisplay();
            p.width = d.getWidth(); //设置dialog的宽度为当前手机屏幕的宽度
            dialogWindow.setAttributes(p);
        }
    }

    @Override
    public void dismiss() {
        rootView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        super.dismiss();
    }
}
